package com.playball.lottery.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.playball.lottery.R;

/**
 * Created by aa on 2017/4/16.
 */

public class PermissionUitl {
    public static final int PERMISSON_REQUESTCODE=0;
    private Context context;
    public PermissionUitl(Context context){
        this.context=context;
    }
    public void data0(Activity acticity, String[] str) {
        if (!(Build.VERSION.SDK_INT < Build.VERSION_CODES.M)) {
            boolean a = false;
            for (String i : str) {
                if (ContextCompat.checkSelfPermission(context, i) != PackageManager.PERMISSION_GRANTED) {
                    a = true;
                }
            }
            if (a) {
                for (String j : str) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(acticity, j)) {
                        a = true;
                    }
                }
            }else {
                return;
            }
            if (a) {
                ActivityCompat.requestPermissions(acticity, str, PERMISSON_REQUESTCODE);
            } else {
                showDialog();
            }
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSON_REQUESTCODE) {
            boolean c = false;
            for (int result : grantResults) {//* 检测是否说有的权限都已经授权
                if (result != PackageManager.PERMISSION_GRANTED) {
                    c = true;
                }
            }
            if (c) {
                showDialog();
            }
        }
    }

    /**
     * 显示提示信息
     *
     * @since 2.5.0
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.notifyTitle);
        builder.setMessage(R.string.notifyMsg);

        // 拒绝, 退出应用
        builder.setNegativeButton(R.string.net_no,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.setPositiveButton(R.string.setting,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {//* 启动应用的设置
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + context.getPackageName()));
                        context.startActivity(intent);
                    }
                });
        builder.setCancelable(false);
        builder.show();
    }
}
